/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.user;

import com.servengine.struts.ValidatorForm;

import java.lang.reflect.InvocationTargetException;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;
import org.apache.commons.beanutils.PropertyUtils;

public class PreferencesForm extends ValidatorForm
{
	boolean hasGuestUser,newUserNotify,selfSignUp,requiresSignUpConfirmation;
	int sessionRememberDays;
	Map<String,ExtraUserdataField> extraUserdataFields;
	String newExtraUserdataFieldName;
	Integer[] removeExtraUserdataFieldId;

	public void reset(ActionMapping mapping, HttpServletRequest request)
	{
		super.reset(mapping,request);
	  if (extraUserdataFields==null)
	    extraUserdataFields = new HashMap<String,ExtraUserdataField>();
	}

	public void setExtraUserdataFields(Collection<ExtraUserdataField> fields) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
	  extraUserdataFields = new HashMap<String,ExtraUserdataField>();
		for (ExtraUserdataField field: fields)
		{
		  ExtraUserdataField fieldDTO = new ExtraUserdataField();
			fieldDTO.setId(field.getId());
			PropertyUtils.copyProperties(fieldDTO,field);
		  extraUserdataFields.put(""+field.getId(),fieldDTO);
		}
	}

	public Collection<ExtraUserdataField> getExtraUserdataFields()
	{
		return extraUserdataFields.values();
	}

	protected ExtraUserdataField getExtraUserdataField(String id)
	{
		if (!extraUserdataFields.containsKey(id))
		{
		  ExtraUserdataField dataField = new ExtraUserdataField();
			dataField.setId(new Integer(id));
			extraUserdataFields.put(id,dataField);
		}
		return extraUserdataFields.get(id);
	}

	public void setExtraUserdataFieldName(String id, String fieldname)
	{
		getExtraUserdataField(id).setName(fieldname);
	}

	public String getExtraUserdataFieldName(String id)
	{
		return getExtraUserdataField(id).getName();
	}

	public void setExtraUserdataFieldRequired(String id, boolean required)
	{
	  getExtraUserdataField(id).setRequired(required);
	}

	public boolean getExtraUserdataFieldRequired(String id)
	{
	  return getExtraUserdataField(id).isRequired();
	}

	public void setExtraUserdataFieldPublic(String id, boolean publicfield)
	{
	  getExtraUserdataField(id).setPublicfield(publicfield);
	}

	public boolean getExtraUserdataFieldPublic(String id)
	{
	  return getExtraUserdataField(id).isPublicField();
	}

	public void setExtraUserdataFieldOptions(String id, String options)
	{
	  getExtraUserdataField(id).setOptions(options);
	}

	public String getExtraUserdataFieldOptions(String id)
	{
	  return getExtraUserdataField(id).getOptions();
	}

	public void setNewExtraUserdataFieldName(String newFieldName)
	{
		newExtraUserdataFieldName = newFieldName;
	}

	public String getNewExtraUserdataFieldName()
	{
		return newExtraUserdataFieldName;
	}

	public void setRemoveExtraUserdataFieldId(Integer[] removeId)
	{
		this.removeExtraUserdataFieldId = removeId;
	}

	public Integer[] getRemoveExtraUserdataFieldId()
	{
		return removeExtraUserdataFieldId;
	}

	public void setHasGuestUser(boolean hasGuestUser)
	{
		this.hasGuestUser = hasGuestUser;
	}

	public boolean isHasGuestUser()
	{
		return hasGuestUser;
	}

	public void setNewUserNotify(boolean newUserNotify)
	{
		this.newUserNotify = newUserNotify;
	}

	public boolean isNewUserNotify()
	{
		return newUserNotify;
	}

	public void setSelfSignUp(boolean selfSignUp)
	{
		this.selfSignUp = selfSignUp;
	}

	public boolean isSelfSignUp()
	{
		return selfSignUp;
	}

	public void setRequiresSignUpConfirmation(boolean requiresSignUpConfirmation)
	{
		this.requiresSignUpConfirmation = requiresSignUpConfirmation;
	}

	public boolean isRequiresSignUpConfirmation()
	{
		return requiresSignUpConfirmation;
	}

	public void setSessionRememberDays(int sessionRememberDays)
	{
		this.sessionRememberDays = sessionRememberDays;
	}

	public int getSessionRememberDays()
	{
		return sessionRememberDays;
	}
	}
